package com.kk.score.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.kk.score.utils.PageUtils;
import com.kk.score.utils.R;
import com.kk.score.utils.SpringSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.kk.score.entity.TaskEntity;
import com.kk.score.service.TaskService;


/**
 * @author KK
 * @email 409592048@qq.com
 * @date 2022-11-02 12:28:00
 */
@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = taskService.queryPage(params);

        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @GetMapping("/personList")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public R personList(@RequestParam Map<String, Object> params) {
        PageUtils page = taskService.queryPersonList(params);
        return R.ok().put("data", page);
    }


    /**
     * 打分
     */
    @PostMapping("/grade")
    @PreAuthorize("hasAnyRole('ROLE_USER')")
    public R grade(@RequestBody TaskEntity taskEntity) {
        TaskEntity byId = taskService.getById(taskEntity.getUid());
        if (!byId.getUserUid().equals(SpringSecurityUtils.getUid())) {
            return R.error("非法操作！");
        }
        //判断次数是否用完
        if (byId.getCount() == 0){
            return R.error("每一个报告分数只允许修改3次哦！");
        }
        if (byId.getStatus() == 2) {
            return R.error("未在规定时间！");
        }
        Date date = new Date();
        byId.setContext(taskEntity.getContext());
        byId.setCompleteTime(date);
        byId.setUpdateTime(date);
        byId.setScore(taskEntity.getScore());
        byId.setStatus(1);
        byId.setCount(byId.getCount() - 1);
        taskService.updateById(byId);
        return R.ok();
    }
}
